package orz.itrip.trade.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import orz.itrip.DtoUtils;
import orz.itrip.dto.Dto;
import orz.itrip.mapper.HotelOrderMapper;
import orz.itrip.mapper.HotelTempStoreMapper;
import orz.itrip.pojo.HotelOrder;
import orz.itrip.trade.config.WXPay.WXPayConstants;
import orz.itrip.trade.config.WXPay.WXPayRequest;
import orz.itrip.trade.config.WXPay.WXPayUtil;
import orz.itrip.trade.service.GetInstanceService;
import orz.itrip.trade.service.TradeService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class TradeServiceImpl implements TradeService {
    @Resource
    private HotelOrderMapper hotelOrderMapper;
    @Resource
    private GetInstanceService getInstanceService;
    @Override
    public Dto payViaWX(String orderNo) {
        HotelOrder hotelOrder = new HotelOrder();
        hotelOrder.setOrderNo(orderNo);
        hotelOrder=hotelOrderMapper.selectOne(hotelOrder);
        //此数据固定不变
        String appid="wxab8acb865bb1637e";
        String mch_id="11473623";
        String key="2ab9071b06b9f739b950ddb41db2690d";
        Map<String,String> data=new HashMap<>();
        data.put("body",hotelOrder.getHotelName());//商品名称
        data.put("out_trade_no", hotelOrder.getOrderNo());//订单号
        data.put("total_fee","1");//分
        data.put("spbill_create_ip","123.12.12.123");
        data.put("notify_url","http://epybij.natappfree.cc/trade/api/wxpay/wxnotify");
        data.put("trade_type","NATIVE");//指定为扫描支付
        data.put("product_id",hotelOrder.getRoomId().toString());
        data.put("appid",appid);
        data.put("mch_id",mch_id);
        data.put("nonce_str", WXPayUtil.generateNonceStr());
        data.put("sign_type", "HMAC-SHA256");
        try {
            WXPayUtil.generateSignedXml(data,key,WXPayConstants.SignType.HMACSHA256);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url="https://"+WXPayConstants.DOMAIN_API+ WXPayConstants.UNIFIEDORDER_URL_SUFFIX;
        String resultXML = null;
        Map<String,String> resultMap = null;
        try {
            resultXML= WXPayRequest.sendMsgToWxOnlyOne(url,WXPayUtil.mapToXml(data));
            System.out.println(resultXML);
            resultMap= WXPayUtil.xmlToMap(resultXML);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //封装传递给前端的数据
        Map<String,Object> dataVo = new HashMap<>();
        dataVo.put("codeUrl",resultMap.get("code_url"));
        dataVo.put("payAmount",hotelOrder.getPayAmount());
        dataVo.put("count",hotelOrder.getCount());
        dataVo.put("hotelName",hotelOrder.getHotelName());
        dataVo.put("roomId",hotelOrder.getRoomId());
       /* wx_prepay_id = resultMap.get("prepay_id");
        System.out.println("微信传递给商户的预处理码："+ wx_prepay_id);*/
        return DtoUtils.getSuccesWithData(dataVo,"支付成功");
    }

    @Override
    public void updateData(String out_trade_no, String prepay_id, int i) {
        HotelOrder hotelOrder = getInstanceService.getHotelOrder(out_trade_no);
        hotelOrder.setOrderStatus(2);
        hotelOrder.setTradeNo(prepay_id);
        hotelOrder.setPayType(1);
        getInstanceService.updateTempStore(hotelOrder);
    }

    @Override
    public Dto queryorderstatus(String orderNo) {
        HotelOrder hotelOrder = new HotelOrder();
        hotelOrder.setOrderNo(orderNo);
        hotelOrder = hotelOrderMapper.selectOne(hotelOrder);
        if (hotelOrder!=null){
            return DtoUtils.getSuccesWithData(hotelOrder,null);
        }
        return DtoUtils.getFalseDto(null,"70001");
    }
}
